package cn.itcast.dao.impl;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

import cn.itcast.dao.IAdminDao;
import cn.itcast.entity.Admin;

public class AdminDao implements IAdminDao{

	// IOC容器(依赖)注入SessionFactory对象
	private SessionFactory sessionFactory;
	
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}
	
	@Override
	public void save(Admin admin) {
		sessionFactory.getCurrentSession().save(admin);
	}
	
	@Override
	public Admin findByAdmin(Admin admin) {
		Session session = sessionFactory.getCurrentSession();
		Query q = session.createQuery("from Admin where adminName = ? and pwd = ?");
		q.setString(0, admin.getAdminName());
		q.setString(1, admin.getPwd());
		return (Admin) q.uniqueResult();
	}

}
